package com.tnovoselec.android.travelassistant.activity;


import android.app.ProgressDialog;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;

import com.google.android.maps.GeoPoint;
import com.j256.ormlite.dao.Dao;
import com.tnovoselec.android.common.async.Async;
import com.tnovoselec.android.common.async.listener.Outcome;
import com.tnovoselec.android.common.async.notif.NotifierFactory;
import com.tnovoselec.android.lbs.location.MyLocationService;
import com.tnovoselec.android.travelassistant.Constant;
import com.tnovoselec.android.travelassistant.R;
import com.tnovoselec.android.travelassistant.adapter.AbstractAdapter;
import com.tnovoselec.android.travelassistant.adapter.BankomatiAdapter;
import com.tnovoselec.android.travelassistant.async.BankomatiRefresher;
import com.tnovoselec.android.travelassistant.async.BankomatiSearcher;
import com.tnovoselec.android.travelassistant.dto.BankomatiResultHolder;
import com.tnovoselec.android.travelassistant.dto.IGeoPoint;
import com.tnovoselec.android.travelassistant.overlay.BankomatiAROverlayNavigation;
import com.tnovoselec.android.travelassistant.overlay.BankomatiOverlay;
import com.tnovoselec.android.travelassistant.overlay.BankomatiOverlay.BankomatSelectedListener;
import com.tnovoselec.android.ui.quickaction.ActionItem;
import com.tnovoselec.android.ui.quickaction.QuickAction;

public class BankomatiMapActivity extends AbstractMapActivity implements BankomatSelectedListener {

	public static String TAG = BankomatiMapActivity.class.getSimpleName();

	private static final int ZABA_ID = 1;
	private static final int PBZ_ID = 2;
	private static final int HPB_ID = 3;
	private static final int HYPO_ID = 4;
	private static final int SPLITSKA_ID = 5;
	private static final int OTP_ID = 6;
	private static final int ERSTE_ID = 7;
	private static final int RBA_ID = 8;
	private static final int ALL_ID = 9;

	protected synchronized void search(final GeoPoint geoPoint, final VIEWS view) {

		dialog = ProgressDialog.show(this, "", super.getResources().getText(R.string.please_wait), true);

		Async.doAsync(null, new BankomatiSearcher(BankomatiMapActivity.this, poiDao, geoPoint, activeType), new Outcome<Void, Boolean>() {

			@SuppressWarnings("synthetic-access")
			@Override
			protected void succeeded(Void key, Boolean result) {
				dialog.dismiss();

				if (result.booleanValue()) {
					if (view == VIEWS.MAP || view == VIEWS.LIST) {
						Drawable mapPin = getResources().getDrawable(R.drawable.map_pin);
						mapOverlay = new BankomatiOverlay(BankomatiMapActivity.this, mapPin, BankomatiResultHolder.getAllBanks(), mapView);
						((BankomatiOverlay)mapOverlay).setListener(BankomatiMapActivity.this);
						mapOverlay.adjustMapViewToPOIs(mapView, null);
						mapView.getOverlays().clear();
						mapView.getOverlays().add(mapOverlay);
						mapView.getOverlays().add(locationOverlay);
						updateList();
					} else if (view == VIEWS.AR) {

						BankomatiAROverlayNavigation overlay = new BankomatiAROverlayNavigation(arAugmentedView);
						overlay.setPoiARList(BankomatiResultHolder.getAllBanks());
						arAugmentedView.removeAllOverlays();
						arAugmentedView.addOverlay(overlay);
						arAugmentedView.setLocation(MyLocationService.getLastKnownLocation(BankomatiMapActivity.this));
					}
				} else {
					// ImageToast.show(HomeActivity.this, R.drawable.icon,
					// R.string.no_result);
				}

			}

			@Override
			protected void failed(Void key, Throwable error) {
				dialog.dismiss();

				Log.e(TAG, "Exception: ", error);
			}
		}, NotifierFactory.newThreadSpecificNotifier(handler));
	}


	@Override
	public void onAnimationStart(Animation animation) {
		// NIX

	}

	@Override
	public void onAnimationEnd(Animation animation) {
		if (animation == animHide) {
			searchContainer.setVisibility(View.GONE);
			isSearchActivated = false;
			if (query != null) {
				query.setText("");
			}
		}

	}

	@Override
	public void onAnimationRepeat(Animation animation) {
		// NIX

	}

	@Override
	public void onBankomatSelected() {
		hideSearchPopup();

	}

	
	@Override
	public void onItemClick(QuickAction source, int pos, int actionId) {
		if (source == quickAction) {
			Log.i(TAG, "item clicked: " + actionId);
			if (actionId == ZABA_ID) {
				activeType = Constant.ZABA_REGEX;
			} else if (actionId == PBZ_ID) {
				activeType = Constant.PBZ_REGEX;
			} else if (actionId == HPB_ID) {
				activeType = Constant.HPB_REGEX;
			} else if (actionId == HYPO_ID) {
				activeType = Constant.HYPO_REGEX;
			} else if (actionId == ERSTE_ID) {
				activeType = Constant.ERSTE_REGEX;
			} else if (actionId == RBA_ID) {
				activeType = Constant.RBA_REGEX;
			} else if (actionId == SPLITSKA_ID) {
				activeType = Constant.SPLITSKA_REGEX;
			} else if (actionId == OTP_ID) {
				activeType = Constant.OTP_REGEX;
			} else if (actionId == ALL_ID) {
				activeType = "";
			}
			// search(MyLocationService.getLastKnownGeoPoint(this), VIEWS.MAP);
			// search(mapView.getCurrentMiddlePoint(), VIEWS.MAP);
			super.onItemClick(source, pos, actionId);
		}

	}

	@Override
	public Dao<IGeoPoint, String> getDao() {
		try {
			return this.getHelper().getBankomatDao();
		} catch (Exception e) {
			Log.e(TAG, "dao fetch failed");
		}
		return null;
	}

	@Override
	public AbstractAdapter<IGeoPoint> getAdapter() {
		// TODO Auto-generated method stub
		return new BankomatiAdapter(this);
	}

	@Override
	public void populateQuickAction() {
		ActionItem zabaAction = new ActionItem(ZABA_ID, getString(R.string.zaba_title), getResources().getDrawable(R.drawable.zaba_icon));
		ActionItem pbzAction = new ActionItem(PBZ_ID, getString(R.string.pbz_title), getResources().getDrawable(R.drawable.pbz_icon));
		ActionItem hpbAction = new ActionItem(HPB_ID, getString(R.string.hpb_title), getResources().getDrawable(R.drawable.hpb_icon));
		ActionItem ersteAction = new ActionItem(ERSTE_ID, getString(R.string.erste_title), getResources().getDrawable(R.drawable.erste_icon));
		ActionItem hypoAction = new ActionItem(HYPO_ID, getString(R.string.hypo_title), getResources().getDrawable(R.drawable.hypo_icon));
		ActionItem rbaAction = new ActionItem(RBA_ID, getString(R.string.rba_title), getResources().getDrawable(R.drawable.rba_icon));
		ActionItem splitskaAction = new ActionItem(SPLITSKA_ID, getString(R.string.splitska_title), getResources().getDrawable(R.drawable.splitska_icon));
		ActionItem otpAction = new ActionItem(OTP_ID, getString(R.string.otp_title), getResources().getDrawable(R.drawable.otp_icon));
		ActionItem allAction = new ActionItem(ALL_ID, getString(R.string.all_banks_title), getResources().getDrawable(R.drawable.all));

		quickAction.addActionItem(zabaAction);
		quickAction.addActionItem(pbzAction);
		quickAction.addActionItem(hpbAction);
		quickAction.addActionItem(ersteAction);
		quickAction.addActionItem(hypoAction);
		quickAction.addActionItem(splitskaAction);
		quickAction.addActionItem(rbaAction);
		quickAction.addActionItem(otpAction);
		quickAction.addActionItem(allAction);
		quickAction.setOnActionItemClickListener(this);

	}

	@Override
	public Runnable getUpdateMapRunnable() {
		return new Runnable() {

			@SuppressWarnings("synthetic-access")
			@Override
			public void run() {
				Drawable mapPin = getResources().getDrawable(R.drawable.map_pin);
				mapOverlay = new BankomatiOverlay(BankomatiMapActivity.this, mapPin, BankomatiResultHolder.getAllBanks(), mapView);
				((BankomatiOverlay)mapOverlay).setListener(BankomatiMapActivity.this);
				// bankomatiOverlay.adjustMapViewToPOIs(mapView, null);
				mapView.getOverlays().clear();
				mapView.getOverlays().add(mapOverlay);
				mapView.getOverlays().add(locationOverlay);
				mapView.invalidate();
				updateList();
			}
		};
	}

	@Override
	public void onMapMoved(GeoPoint tl, GeoPoint br) {
	     Async.doAsync(null, new BankomatiRefresher(this, poiDao, tl, br, activeType), new Outcome<Void, Boolean>() {

             @SuppressWarnings("synthetic-access")
             @Override
             protected void succeeded(Void key, Boolean result) {
                     if (result.booleanValue()) {
                             handler.removeCallbacks(updateMap);
                             handler.postDelayed(updateMap, 1000L);
                     } else {
                             // NIX
                     }

             }

             @Override
             protected void failed(Void key, Throwable t) {
                     Log.e(TAG, t.getMessage());

             }
     }, NotifierFactory.newThreadSpecificNotifier(handler));

	}

	@Override
	public void updateList() {
		adapter.setData(BankomatiResultHolder.getAllBanks());
	}

}
